﻿using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;

namespace MethodCaching.Weaving
{
    /// <summary>
    /// A <see cref="IMethodSelector"/> implementation that uses 
    /// a function delegate to determine the target methods.
    /// </summary>
    public class LambdaMethodSelector :  IMethodSelector
    {
        private readonly Func<MethodDefinition, bool> _selector;

        /// <summary>
        /// Initializes a new instance of the <see cref="LambdaMethodSelector"/> class.
        /// </summary>
        /// <param name="selector">The function delegate used to select the target methods.</param>
        public LambdaMethodSelector(Func<MethodDefinition,bool> selector)
        {
            _selector = selector;
        }

        /// <summary>
        /// Returns a list of methods from the target <paramref name="typeDefinition"/>. 
        /// </summary>
        /// <param name="typeDefinition">The type that contains the target methods.</param>
        /// <returns>The list of selected methods.</returns>
        public IEnumerable<MethodDefinition> Select(TypeDefinition typeDefinition)
        {
            return typeDefinition.Methods.Where(_selector).ToList();
        }
    }
}
